const router = require('koa-router')()
const DB = require('../other/DB');
const uuid = require('uuid').v1;
const util = require('../other/util')
router.prefix('/api/admin')

router.all('/', (ctx, next) => {
    if (ctx.userInfo.permission == 'admin') {
        next();
    } else {
        throw new Error('权限错误');
    };
});

router.get('/topic/list', async (ctx) => {
    ctx.body = await DB.query(`SELECT * FROM topic WHERE desable = 0 ORDER BY create_time`)
});

router.put('/topic/create', async (ctx) => {
    const paras = {
        ...util.ObjToUnderline(ctx.request.body),
        id: uuid()
    };
    await DB.query(`INSERT INTO topic SET ?`, paras)
    ctx.body = {
        message: '创建成功'
    };
});

router.post('/topic/Appraise', async ctx => {
    const paras = {
        ...util.ObjToUnderline(ctx.request.body),
        id: uuid()
    };
    await DB.query(`INSERT INTO inspect SET ?`, paras)
    ctx.body = {
        message: '评分成功'
    };
})

router.get('/user/list', async function (ctx) {
    var users = await DB.query(`SELECT * FROM \`user\``);
    ctx.body = users;
});

router.put('/user/update', async function (ctx) {
    var paras = {
        username: ctx.request.body.username,
        password: ctx.request.body.password,
        nickname: ctx.request.body.nickname,
        permission: ctx.request.body.permission,
        desable: ctx.request.body.desable,
    };
    if (!paras.username) throw new Error('用户名不能为空');
    if (!['admin', 'user', 'watcher'].includes(paras.permission)) throw new Error('权限类型错误');

    await DB.query(`UPDATE \`user\` SET ? WHERE username = ?`, [paras, paras.username])
    ctx.body = {
        message: '修改成功'
    };
});


router.post('/inspect/list', async ctx => {
    ctx.body = await DB.query(`SELECT
	i.code_content \`Code\`,
	i.id,
	i.inspectA,
	i.inspectB,
	i.inspectC,
	i.inspectD,
	i.inspectE,
    i.submit_time,
    i.score_time,
	i.total_points total,
	i.update_time update_time,
	i.draft,
	u.nickname,
    i.topic_id,
	u.username 
FROM
\`user\` u LEFT JOIN (SELECT * FROM inspect WHERE topic_id = ?) i ON u.username = i.user_id
WHERE
u.permission = 'user' AND 
u.desable = 0 ORDER BY submit_time`, [ctx.request.body.id]);
})

router.post('/inspect/upload', async ctx => {
    var _T = new Date();
    var paras = {
        inspectA: ctx.request.body.inspectA || 0,
        inspectB: ctx.request.body.inspectB || 0,
        inspectC: ctx.request.body.inspectC || 0,
        inspectD: ctx.request.body.inspectD || 0,
        inspectE: ctx.request.body.inspectE || 0,
        total_points: ctx.request.body.total || 0,
        draft: 1,
        score_time: `${_T.getFullYear()}-${_T.getMonth() + 1}-${_T.getDate()} ${_T.getHours()}:${_T.getMinutes()}:${_T.getSeconds()}`,
    };
    await DB.query(`UPDATE inspect SET ? WHERE id = ?`, [paras, ctx.request.body.id]);
    ctx.body = {
        message: '评分成功'
    };
})

router.post('/inspect/backstatus', async ctx => {
    var paras = {
        inspectA: null,
        inspectB: null,
        inspectC: null,
        inspectD: null,
        inspectE: null,
        total_points: null,
        draft: 0
    };
    await DB.query(`UPDATE inspect SET ? WHERE id = ?`, [paras, ctx.request.body.id]);
    ctx.body = {
        message: '退回成功'
    };
});

router.post('/inspect/savecode', async ctx => {
    var paras = {
        code_content: ctx.request.body.Code
    };
    await DB.query(`UPDATE inspect SET ? WHERE id = ?`, [paras, ctx.request.body.id]);
    ctx.body = {
        message: '保存成功'
    };
});

module.exports = router